package org.jsoft.panel;

import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jsoft.constants.Constants;
import org.jsoft.constants.ImageConstants;
import org.jsoft.model.Bomb;
import org.jsoft.model.Earth;
import org.jsoft.model.Sun;
import org.jsoft.test.TestFor;

public class BgFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JPanel panel = new JPanel(){
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		protected void paintComponent(Graphics g) {
			g.drawImage(ImageConstants.bg, 0, 0, this.getWidth(), this.getHeight(), null);
		}
	};
	
	public static Earth earth;
	public static Sun sun;
	private Bomb blast;
	private TestFor tf = new TestFor();

	public void init() {
		this.setSize(1000, 650);
		this.setLocation(0, 0);
		this.setVisible(true);
		this.setResizable(false);
		this.setLayout(null);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		
		panel.setLocation(0, 0);
		panel.setSize(1000, 650);
		panel.setLayout(null);
		
		this.earth = new Earth();
		earth.init();
		earth.setLocation(0, 0);
		panel.add(earth);
		
		this.sun = new Sun();
		sun.init();
		int solarX = (this.getWidth() - sun.getWidth()) / 2;
		int solarY = (this.getHeight() - sun.getHeight()) / 2;
		sun.setLocation(solarX, solarY);
		panel.add(sun);
		tf.testFor();
		
		this.blast = new Bomb();
		blast.init();
		blast.setLocation(0, 0);
//		panel.add(blast);
		
		this.add(panel);
		new MyThread().start();
	}

	public void paint(Graphics g) {
		if(sun!=null && earth!=null){
			earth.moving();
		}
		super.paint(g);
	}

	boolean runFlag=true;
	class MyThread extends Thread {

		public void run() {
			while (runFlag) {
				repaint();
				try {
					sleep(Constants.FPS);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}

	}
}
